//
//  ChatRoomView.m
//  UniverseProject
//
//  Created by king on 2021/6/17.
//  Copyright © 2021 Cloud2209. All rights reserved.
//


#import "ChatRoomView.h"
#import "ChatRoomCell.h"

@interface ChatRoomView() <UITableViewDelegate,UITableViewDataSource>

@property (nonatomic ,weak) UITableView *tableView;
@property (nonatomic ,strong) NSMutableArray *dataArr;
@property (assign,nonatomic) NSInteger pageNo;

@end

@implementation ChatRoomView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.clipsToBounds = YES;
        NSMutableArray *dataArr = [NSMutableArray array];
        for (int i = 0; i < 30; i ++) {
            [dataArr addObject: [NSString stringWithFormat:@"%ld",i]];
        }
        self.dataArr = dataArr;
        [self setupTabWithFrame:frame];
    }
    return self;
}

#pragma mark - 表格信息处理
/**
 设置表格
 */
- (void)setupTabWithFrame:(CGRect)frame{
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:self.bounds style:UITableViewStylePlain];
    [self addSubview:tableView];
    
    self.tableView = tableView;
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.showsVerticalScrollIndicator = NO;
    tableView.bounces = NO;
    self.tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.estimatedRowHeight = 0;
    self.tableView.estimatedSectionHeaderHeight = 0;
    self.tableView.estimatedSectionFooterHeight = 0;
    
    kWeakSelf(self);
//    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
//        weakself.pageNo += 1;
//        [weakself getUserMessageList];
//    }];
//
//    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
//        weakself.pageNo = 1;
//        [weakself getUserMessageList];
//    }];
    tableView.ly_emptyView = [LGDIYEmptyView diyNoDataEmptyImageName:@"newjbflszImage" Title:@"暂无数据" content:@""];
}

#pragma mark -UITableView代理方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ChatRoomCell *cell = [ChatRoomCell cellWithTableView:tableView];
    cell.countLab.text = [NSString stringWithFormat:@"%ld",indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

}

-(void)getUserMessageList
{
    NSMutableDictionary *param = [NSMutableDictionary new];
    param[@"page"] = @(self.pageNo);
    param[@"type"] = @(2);//消息类型：0关注1点赞2收藏
    NSString *url = @"api/user/notification";
    
    
}

- (void)layoutSubviews{
    [super layoutSubviews];
    
}




#pragma mark - 企业信息处理

/// 弹出企业信息
- (void)showEnterpriseInfo{
//    NSLog(@"调用");
//    如果企业信息不存在
    if (self.timer) {//如果定时器存在,则证明已有企业展示信息
        //方法1 ,区分动画状态,做精细操作 ,方法2 做一个队列执行的操作
        
        //精细操作,判断table高度,如果是完整高度则是展开状态 ,如果是收缩的高度 则是,信息已弹出状态
        NSLog(@"%f",self.tableView.height); //只有展开收缩两种数值,没有中间值
        
        
    }else{
        CGFloat viewX = 15;
        CGFloat viewY = self.height- 85;
        EnterpriseInfoView *view = [[EnterpriseInfoView alloc] initWithFrame:CGRectMake(viewX, viewY, 100, 80)];
        view.backgroundColor = [UIColor blueColor];
        [self addSubview:view];
        self.enterpriseInfoView = view;
        
        //锚点设置之后,会导致frame改变,所以要重新计算
        CGPoint oldOrigin = self.enterpriseInfoView.frame.origin;
        CGPoint anchorPoint = CGPointMake(0, 1);
        self.enterpriseInfoView.layer.anchorPoint = anchorPoint;
        CGPoint newOrigin = self.enterpriseInfoView.frame.origin;
        CGPoint transition;
        transition.x = newOrigin.x - oldOrigin.x;
        transition.y = newOrigin.y - oldOrigin.y;
        self.enterpriseInfoView.transform = CGAffineTransformMakeScale(0, 0);
        self.enterpriseInfoView.center = CGPointMake (self.enterpriseInfoView.center.x - transition.x, self.enterpriseInfoView.center.y - transition.y);
        [UIView animateWithDuration:1.f animations:^{
            self.tableView.y -= 80;
//            self.enterpriseInfoView.y -=80;
            view.transform = CGAffineTransformMakeScale(1, 1);
        } completion:^(BOOL finished) {
            self.tableView.y = 0;
            self.tableView.height -= 80;
        }];
        
        
        //创建Timer
        NSTimer *timer = [NSTimer timerWithTimeInterval:3.0 target:self selector:@selector(hideEnterpriseInfo) userInfo:nil repeats:NO];
        //使用NSRunLoopCommonModes模式，把timer加入到当前Run Loop中。
        [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
        self.timer = timer;
    }
    
    
//  __weak typeof(self)WeakSelf = self;
//  self.timer = [NSTimer scheduledTimerWithTimeInterval:5.f repeats:NO block:^(NSTimer * _Nonnull timer) {
//      NSLog(@"%s",__func__);
//      [WeakSelf hideEnterpriseInfo];
//  }];
    
    
//   dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(4.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
////        [self hideEnterpriseInfo];
//       NSLog(@"%@",self.timer);
//   });
    
}


/// 隐藏企业信息
- (void)hideEnterpriseInfo{
    
    self.tableView.height += 80;
    [UIView animateWithDuration:0.5f animations:^{
        self.enterpriseInfoView.y = self.bottom;
    } completion:^(BOOL finished) {
        [self.enterpriseInfoView removeFromSuperview];
        [self.timer invalidate];
        self.timer = nil;
//        NSLog(@"%@",self.timer);
    }];
}



@end
